package ListNode;
// 19-删除链表倒数第n个节点-middle
// meth1: fast寻找第n个节点，fast.next == null时，此时slow指向倒数第n个节点的pre节点
// meth2: fast寻找第n+1个节点，fast == nlll时，slow指向倒数第n个节点的pre节点
import java.util.Scanner;

public class RemoveNthFromEnd{
    public static class ListNode{
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int val){
            this.val = val;
        }
        ListNode(int val, ListNode next){
            this.val = val;
            this.next = next;
        }
    }
    public static ListNode removeNthFromEnd(ListNode head, int n){
        // 哨兵节点
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        // 快慢指针
        ListNode fast = dummy;
        ListNode slow = dummy;
        // 寻找第n个节点
        for(int i = 0; i < n; i++){
            fast = fast.next;
        }
        // 寻找倒数第n个节点的pre节点
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        // 删除倒数第n个节点
        slow.next = slow.next.next;
        return dummy.next;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s = sc.nextLine().split(",");
        int n = sc.nextInt();
        ListNode head = new ListNode(Integer.parseInt(s[0]));
        ListNode cur = head;
        for(int i = 1; i < s.length; i++){
            cur.next = new ListNode(Integer.parseInt(s[i]));
            cur = cur.next;
        }
        ListNode res = removeNthFromEnd(head, n);
        while(res != null){
            System.out.print(res.val + " -> ");
            res = res.next;
        }
        sc.close();
    }
}